<template>
  <div class="login-page">
    <van-nav-bar title="账号登录" left-arrow @click-left="onClickLeft" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        size="large"
        name="username"
        placeholder="请输入账号"
        :rules="[
        { required: true, message: '请输入账号' },
         { pattern: /^\w{5,}$/, message: '用户名至少包含5个字符' }]"
      />
      <van-field
        v-model="password"
        size="large"
        type="password"
        name="password"
        placeholder="请输入密码"
        :rules="[
        { required: true, message: '请输入密码' },
         { pattern: /^\w{6,}$/, message: '密码至少包含6个字符' }
         ]"
      />
      <div style="margin: 18px">
        <van-button block type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>

    <!-- 跳转路由 -->

    <router-link class="link" to="/register">还没有账号，去注册~</router-link>
  </div>
</template>

<script>
import { login } from '@/api/user'
import { setToken } from '@/utils/storage'

export default {
  name: 'login-page',
  data () {
    return {
      username: 'leslie',
      password: '123456'
    }
  },
  methods: {
    async onSubmit (values) {
      // console.log('submit', values)
      // console.log(values)
      // 获取token
      const data = await login(values)
      console.log(data.body.token)

      // 登录成功后 存储token 跳转页面
      setToken(data.body.token)
      this.$toast.success('登录成功')
      this.$router.push('/')
    },

    // 点击箭头返回用户页面
    onClickLeft () {
      this.$router.push('/Layout-User')
    }
  }
}
</script>

<style lang="less" scoped>
.van-nav-bar{
  background-color:#21b97a;
  .van-nav-bar_title{
    font-size: 18px;
    color: white;
  }

}

.van-cell van-field{
   position: relative;
    margin-bottom: 9px;
}
.link {
color: #666;
font-size: 12px;
text-align: center;
 padding-left: 110px;
font-family: "Microsoft YaHei";

 a {
outline: none;
 }
}
.van-form {
.username {
margin: 10px 0;
height: 60px;
line-height: 40px;
font-size: 17px;
}
.password {
 margin: 10px 0;
height: 60px;
line-height: 40px;
font-size: 17px;
}
 font-family: "Microsoft YaHei";
color: #ddd;
margin: 21px 0;
}
</style>
